﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace Tebbee.Business.Server
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "ProductSer" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select ProductSer.svc or ProductSer.svc.cs at the Solution Explorer and start debugging.
    public class ProductSer : IProductSer
    {
        private readonly TebbeeContext context;

        public ProductSer()
        {
            context = new TebbeeContext();
        }

        public int AddNewProduct(Product product)
        {
            ObjectParameter ReturnId = new ObjectParameter("ReturnId", typeof(Int32));
            context.Product_Add(product.Name, product.Description, product.Score, product.ProductImg, product.Quantity, ReturnId);
            return (Int32)ReturnId.Value;
        }

        public ICollection<Product> AllProducts()
        {
            var returnPro = new List<Product>();
            context.Product_All().ToList().ForEach(e => returnPro.Add(new Product()
            {
                PId = e.PId,
                Description = e.Description,
                Score = e.Score,
                Name = e.Name,
                ProductImg = e.ProductImg,
                Quantity = e.Quantity
            }));

            return returnPro;
        }

        public ICollection<Product> ListProducts(int startRow, string searchString)
        {

            var returnPro = new List<Product>();
            context.Product_List(startRow, searchString).ToList().ForEach(e => returnPro.Add(new Product()
            {
                PId = e.PId,
                Description = e.Description,
                Score = e.Score,
                Name = e.Name,
                ProductImg = e.ProductImg,
                Quantity = e.Quantity
            }));

            return returnPro;
        }


        public int EditProduct(Product product)
        {
            ObjectParameter rowEffect = new ObjectParameter("RowEffected", typeof(Int32));
            context.Product_Edit(product.PId, product.Name, product.Description, product.Score, product.ProductImg, product.Quantity, rowEffect);
            return (Int32)rowEffect.Value;
        }

        public int DeleteProduct(int id)
        {
            ObjectParameter rowEffect = new ObjectParameter("RowEffected", typeof(Int32));
            context.Product_Delete(id, rowEffect);
            return (Int32)rowEffect.Value;
        }


        public Product GetProductById(int id)
        {
            Product pro = new Product();
            pro = context.Product_getById(id).Select(e => new Product
            {
                PId = e.PId,
                Description = e.Description,
                Score = e.Score,
                Name = e.Name,
                ProductImg = e.ProductImg,
                Quantity = e.Quantity
            }).Single();
            return pro;
        }
    }
}
